// https://leetcode.cn/problems/long-pressed-name/
// Created by ade on 2022/8/22.
//
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    // 性能虽然优秀，但是代码可读性太差
    bool isLongPressedName1(string name, string typed) {
        int len1 = name.size();
        int len2 = typed.size();
        int j = 0, i = 0;
        for (; i < len1; i++) {
            if (name[i] == typed[j]) {
                j++;
            } else if (j > 0 && typed[j] == typed[j - 1]) {
                j++;
                i--;
            } else {
                return false;
            }
        }
        if (j < len2) {
            while (j < len2) {
                if (typed[j - 1] != typed[j]) {
                    return false;
                }
                j++;
            }
        }

        return j == len2;
    }

    bool isLongPressedName(string name, string typed) {
        int i = 0, j = 0;
        while (j < typed.size()) {
            if (i < name.size() && name[i] == typed[j]) {
                i++;
                j++;
            } else if (j > 0 && typed[j] == typed[j - 1]) {
                j++;
            } else {
                return false;
            }
        }
        return i == name.size();
    }
};

int main() {
    Solution so;
    string name = "vtkgn";
    string typed = "vttkgnn";
    auto a = so.isLongPressedName(name, typed);
    cout << a << endl;
    return 0;
}